package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.service.ISmsService;
import com.ruoyi.system.utils.SmsUtils;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisServer;
import org.springframework.stereotype.Service;

@Service
public class SmsServiceImpl implements ISmsService {

    @Autowired
    private SmsUtils smsUtils;

    @Autowired
    private RedisCache redisCache;

    @SneakyThrows
    @Override
    public void sendCode(String phone) {
        //这里后续要加上一分钟只能发送一次 如果验证完成之后可以无限发送
        Object valueByKey = redisCache.getValueByKey(phone + "send");
        if (valueByKey != null){
            throw new ServiceException("一分钟只能发送一次");
        }
        String code = smsUtils.sendMsg(phone);
        redisCache.saveKeyValueWithTwoMinutesExpiration(phone, code,2);
        redisCache.saveKeyValueWithTwoMinutesExpiration(phone + "send", "1",1);
    }
}
